package com.woniuxy.ams.service.impl;

import com.woniuxy.ams.dao.MenuDao;
import com.woniuxy.ams.dao.RoleDao;
import com.woniuxy.ams.entity.Menu;
import com.woniuxy.ams.entity.User;
import com.woniuxy.ams.service.AuthService;
import com.woniuxy.ams.service.MenuService;
import com.woniuxy.ams.utils.SystemUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.TreeSet;

/**
 * @author fyx
 */
@Service
public class MenuServiceImpl implements MenuService {
    @Resource private RoleDao roleDao;
    @Resource private MenuDao menuDao;
    @Resource private AuthService authService;
    @Override
    public List<Menu> findMenus() {
        User loginUser = SystemUtils.getLoginUser();
        List<String> roles = authService.getRoles(loginUser.getId());
        if(roles.size() == 0){
            roles.add("user");
        }
        List<Integer> perm = roleDao.findPermByRoleName(roles);
        if (perm.size() == 0) {
            perm.add(1);
        }
        List<Menu> menuList = menuDao.findAll(perm);
        TreeSet<Menu> treeSet = new TreeSet<>(menuList);
        menuList.clear();
        menuList.addAll(treeSet);
        return menuList;
    }
}
